package br.ita.doacoes.view.controledoacoes;


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.swingBean.actions.ApplicationAction;
import org.swingBean.descriptor.BeanTableModel;
import org.swingBean.descriptor.GenericFieldDescriptor;
import org.swingBean.descriptor.TableFieldDescriptor;
import org.swingBean.descriptor.XMLDescriptorFactory;
import org.swingBean.gui.JActButton;
import org.swingBean.gui.JBeanPanel;
import org.swingBean.gui.JBeanTable;

import br.ita.doacoes.core.cadastrodoacoes.dao.ItemDAO;
import br.ita.doacoes.core.cadastrodoacoes.dao.TipoItemDAO;
import br.ita.doacoes.core.cadastrodoacoes.dao.impl.ItemDAOImpl;
import br.ita.doacoes.core.cadastrofamilia.dao.DAO;
import br.ita.doacoes.domain.cadastrodoacoes.Item;
import br.ita.doacoes.domain.controledoacoes.DestinoItem;
import br.ita.doacoes.view.cadastrodoacoes.refresher.Refresher;

/**
 * @author Lucas Amaro, Henry e Fleuri
 * @since 04/12/2007
 * 
 * Classe que gera o painel que permite a procura por item a partir de seu nome nas necessidades das familias
 * 
 **/

public class RetiradaItemTemplatePanel extends JPanel {
			
	protected JPanel panelTopo;
	protected JBeanPanel panelBottom;	
	protected JActButton botaoFiltrar;
	protected JActButton botaoListarTodos;
	protected JActButton botaoRetirar;
	protected JLabel labelTipoItem;	
	protected JComboBox comboTipoItem;	
	protected Object currentOnForm;	
	protected BeanTableModel tableModel;
	protected JBeanTable table;

	private String xmlForm;
	private String xmlTable;
	private String formName;
	private String tableName;
	private Class classe1;
	private Class classe2;
	private ItemDAO daoItem;
	private TipoItemDAO daoTipoItem;	
	private DAO daoDestinoItem;
	
	public void init() {
				
		TableFieldDescriptor tableDescriptor = XMLDescriptorFactory.getTableFieldDescriptor(classe1, xmlTable, tableName);
		tableModel = new BeanTableModel<Item>(tableDescriptor);
		table = new JBeanTable(tableModel);

		daoItem = new ItemDAOImpl(Item.class);
		
		tableModel.orderByProperty("nome");
		tableModel.setBeanList(daoItem.getList());
		table.enableQuickEditing();

		botaoFiltrar = new JActButton("Filtrar",
				new ApplicationAction() {
					public void execute() {						
						
						tableModel.cleanFilter();
						tableModel.setContinuousFiltering(true);
						
						tableModel.filterContains("tipo",comboTipoItem.getSelectedItem().toString());						
						
					 	QuantidadeComparable<Item> quantidadeComparable = new QuantidadeComparable();
						tableModel.filterBetween("quantidade", quantidadeComparable, null);
						
						tableModel.setContinuousFiltering(false);	
											
					}
				});

		botaoListarTodos = new JActButton("Listar Todos",
				new ApplicationAction() {
					public void execute() {
						tableModel.setBeanList(daoItem.getList());
						}
				});
		
		labelTipoItem = new JLabel("Escolha o tipo do item: ");
		comboTipoItem = new JComboBox(new TipoItemCombo());

		GenericFieldDescriptor formdescriptor = XMLDescriptorFactory.getFieldDescriptor(classe2, xmlForm, formName);
		panelBottom = new JBeanPanel<DestinoItem>(classe2, formdescriptor);

		botaoRetirar = new JActButton("Retirar",
				new ApplicationAction() {
					public void execute() {
						
						Item item = (Item) tableModel.getBeanAt(table.getSelectedRow());
						DestinoItem destinoItem = null;
						try {
							destinoItem = (DestinoItem) classe2.newInstance();
						} catch (Exception e ){
							e.printStackTrace();
						}
						panelBottom.populateBean(destinoItem);							
						destinoItem.setItem(item);
						daoDestinoItem.insert(destinoItem);
						
						item.setQuantidade(item.getQuantidade() - destinoItem.getQuantidade());  
						daoItem.update(item);
						
						panelBottom.cleanForm();
						Refresher.refresh();											
					}
				});
			
		panelTopo = new JPanel();
		panelTopo.setLayout(new GridLayout());
		panelTopo.add(labelTipoItem);
		panelTopo.add(comboTipoItem);
		panelTopo.add(botaoFiltrar);
		panelTopo.add(botaoListarTodos);
		panelTopo.add(botaoRetirar);
		panelTopo.setBorder(BorderFactory.createTitledBorder("Retirada de Itens"));
		
		setLayout(new BorderLayout());
		add(panelTopo,BorderLayout.NORTH);
		add(new JScrollPane(table), BorderLayout.CENTER);			
		add(panelBottom,BorderLayout.SOUTH);		
	}

	public void setDao(ItemDAO daoItem, DAO daoDestinoItem) {
		this.daoItem = daoItem;
		this.daoDestinoItem = daoDestinoItem;
	}

	public Class getClasse2() {
		return classe2;
	}

	public void setClasse2(Class classe2) {
		this.classe2 = classe2;
	}

	public Class getClasse1() {
		return classe1;
	}

	public void setClasse1(Class classe1) {
		this.classe1 = classe1;
	}	
	
	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getXmlForm() {
		return xmlForm;
	}

	public void setXmlForm(String xmlForm) {
		this.xmlForm = xmlForm;
	}

	public String getXmlTable() {
		return xmlTable;
	}

	public void setXmlTable(String xmlTable) {
		this.xmlTable = xmlTable;
	}	
}